//
//  MenuModel.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/23.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit
import SwiftyJSON

class MenuModel: NSObject {
    var menu:[JSON]
    var selecte1:Int
    var selecte2:Int
    
    var count1 = 0//第一级的数量
    var count2 = 0
    var count3 = 0
    var menuID1:Array<String> = []//记录第一级的menu的id
    var menuID2:Array<String> = []
    var menuID3:Array<String> = []
    
    
    var menuName1:Array<String> = []//记录第一级的menuName
    var menuName2:Array<String> = []
    var menuName3:Array<String> = []
    init(menu:[JSON],selecte1:Int,selecte2:Int){
        self.menu = menu
        self.selecte1 = selecte1
        self.selecte2 = selecte2
    }
    
    //清空数据 只留一次
    func deleteMenu(){
        count1 = 0//第一级的数量
        count2 = 0
        count3 = 0
        menuID1 = ["","","","","","","","","","","","",""]//记录第一级的menu的id
        menuID2 = ["","","","","","","","","","","","",""]
        menuID3 = []
        
        menuName1 = []//记录第一级的menuName
        menuName2 = []
        menuName3 = []
    }
    
    
    //计算每一级的item数量
    func getMenuItemCount(){
        for i in 0..<menu.count{
            let parent_id = menu[i]["parent_id"].stringValue
            if parent_id == "0"{
                menuID1.insert(menu[i]["menu_id"].stringValue, atIndex: count1)
                menuName1.insert(menu[i]["menu_name"].stringValue, atIndex: count1)
                count1 += 1
            }else if parent_id == menuID1[selecte1]{
                menuID2.insert(menu[i]["menu_id"].stringValue, atIndex: count2)
                menuName2.insert(menu[i]["menu_name"].stringValue, atIndex: count2)
                count2 += 1
            }else if parent_id == menuID2[selecte2]{
                menuID3.insert(menu[i]["menu_id"].stringValue, atIndex: count3)
                menuName3.insert(menu[i]["menu_name"].stringValue, atIndex: count3)
                count3 += 1
            }else{
                continue
            }
        }
    }
    
    
    //得到具体的item
    func getMenuItem(table:Int,row:Int)->String{
        switch table {
        case 1:
            let step1String = menuName1[row]
            return step1String
        case 2:
            let step2String = menuName2[row]
            return step2String
        case 3:
            let step3String = menuName3[row]
            return step3String
        default:
            return ""
        }
    }
    
}















